// app.js
import { config } from "./utils/config.js";

App({
	onLaunch(options) {
		wx.eventBus = require('utils/eventBus.js').eventBus
		// Do something initial when launch.
		const that = this;
		wx.login({
			success: (res) => {
				// 登录
				wx.request({
					url: config.api_base_url + "/smp/login",
					data: { code: res.code },
					method: "POST",
					header: Object.assign(config.header, {
						'content-type': 'application/json',
					}),
					success: result => {
						if (result.data.code == 200) {
							// 登录成功
							if (result.data.data == undefined) {
								that.globalData.isBind = false; // 没有授权绑定过
								that.globalData.username = "";
								that.globalData.token = "";
								that.globalData.isLogin = 2;
								wx.eventBus.post({
									params: {
										isLogin: 2,
										username: ""
									},
									event: 'change'
								})
							}
							else {
								// 存储token 
								that.globalData.token = result.data.data;
								// 获取用户信息
								wx.request({
									url: config.api_base_url + "/wxs/my/getInfo",
									method: "POST",
									header: Object.assign(config.header, {
										'content-type': 'application/json',
										'Authorization': 'Bearer ' + that.globalData.token,
									}),
									success: response => {
										if (response.data.code == 200) {
											that.globalData.username = response.data.data.name == null || response.data.data.name == "" ? "微信授权用户" : response.data.data.name;
											that.globalData.openid = response.data.data.openid;
											that.globalData.mpid = response.data.data.mpid;
											that.globalData.isLogin = 1;
											wx.eventBus.post({
												params: {
													isLogin: 1,
													username: that.globalData.username
												},
												event: 'change'
											})
										}
									}
								})
							}
						}
						else {

						}
					}
				})
			}
		})

	},
	onShow(options) {
		// Do something when show.
		// const userInfo = await isBindUser();

	},
	onHide() {
		// Do something when hide.
	},
	onError(msg) {
		console.log(msg)
	},
	globalData: {
		isBind: true, // 是否绑定过手机号
		isLogin: '0',
		username: "", // 用户名
		token: "", // token
		openid: "",
		mpid: "", // 服务号唯一标识
		innerAudio: {}
	}
})

// {
// 	"pagePath": "pages/tabbar/home/home",
// 	"text": "首页",
// 	"iconPath": "/images/tabbar/home.png",
// 	"selectedIconPath": "/images/tabbar/homeSelect.png"
// },
// {
// 	"pagePath": "pages/tabbar/schedule/schedule",
// 	"text": "约课",
// 	"iconPath": "/images/tabbar/schedule.png",
// 	"selectedIconPath": "/images/tabbar/scheduleSeelct.png"
// },
